multiCall.js
The multiCall.js file has one function executeTryMultiCall() that calls all RPC URLs for a chain in parallel using promises. Workers can can execute it once per chain using their own promises to get responses for all chains at once. See a simple code example in the pricesWorker.
Each call to executeTryMultiCall() can call one or more functions (declared in the parameter contractCalls) but only on a single contract for the chain passed. See the code comments at the top of the multiCall.js file to learn more about how to use multiCall.js.
* @param {*} chain - target chain to call
* @param {*} contractAddress - target contract address on the chain to call
* @param {*} contractAbi - target contract abi
* @param {*} contractCalls - encoded function calls on the contract
* @param {*} updateDbFlag - whether or not to update the RPC provider database info
* @returns new Promise (fulfilled or rejected)
Workflow
-
Execute the
contractCallsfor all chain RPC URLs using internal promises. -
For each RPC URL gets its last block number and networkID and stored then a
callDataarray -
Places the return values for the
contractCallsinto thecallDataarray. -
If all PRC URLs fail then the return promise is tagged as
promise.status === 'rejected'and thecallDatawill contain the error message for each RPC URL. -
If the all RPC URLS succeed then the
callDataarray will have rows for the results of allcallDataand the promise tagged aspromise.status === 'fulfilled'. ThecallDatais returned with the top row having the hightest block number. -
If some RPC URLs fail then the
callDatawill have a mixture ofcallDatavalues and RPC URL error messages and the promise tagged aspromise.status === 'fulfilled'. ThecallDatais returned with the top row having the hightest block number and the RPC URLS that errored at the bottom of thecallDataarray. -
If
updateDbFlagis true then the RPC provider info, for the chain, will be updated in the database CHAINS table."providers": {"rpc": {"https://rpc.apechain.com/http": {"health": true,"runtime": 0.706,"networkId": 33139,"blockNumber": 21227946},...}}